package com.happypets.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.happypets.daoImpl.PetDaoImpl;

public class CheckUserNameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public CheckUserNameServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		PetDaoImpl dao = new PetDaoImpl();
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		Gson gson=new Gson();
		Map<String,String> map = new HashMap<String,String>();
		
		
		String name = request.getParameter("r_userName");
		String sql = "select * from vipinfo a where a.vip_name='"+name+"'";
		System.out.println(name+sql);
		int f = dao.query(sql).size();
		
		if(f == 0 || f > 1){
			map.put("flag", "0");
		}else{
			map.put("flag", "1");
		}
		
		List<Map<String,String>> lm = new ArrayList<Map<String,String>>();
		lm.add(map);
		String json=gson.toJson(lm);
		System.out.println(json);
		out.write(json);
		
		out.flush();
		out.close();
		
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
